#!/usr/bin/env python3

'''从多个工作薄中提取数据 形成 新数据体'''
'''命令格式 ./3.5.2.py 3.5.2 3.5.2output.xls'''
import glob
import os
import sys

from datetime import date
from xlrd import open_workbook,xldate_as_tuple
from xlwt import Workbook

input_folder=sys.argv[1]
output_file =sys.argv[2]
output_workbook=Workbook()
output_worksheet=output_workbook.add_sheet('all_data_workbooks')
data=[]
first_worksheet=True
for input_file in glob.glob(os.path.join(input_folder,'*.xls')):
	print(os.path.basename(input_file))
	with open_workbook(input_file) as workbook:
		for worksheet in workbook.sheets():
			if first_worksheet:
				header_row=worksheet.row_values(0)
				data.append(header_row)
				first_worksheet=False
			for row_index in range(1,worksheet.nrows):
				row_list=[]
				for colum_index in range(worksheet.ncols):
					cell_value =worksheet.cell_value(row_index,colum_index)
					cell_type=worksheet.cell_type(row_index,colum_index)
					if cell_type==3:
						 date_cell=xldate_as_tuple(cell_value,workbook.datemode) 
						 date_cell=date(*date_cell[0:3]).strftime('%m/%d/%Y')
					else:
					       	row_list.append(cell_value)
				data.append(row_list)

for list_index,output_list in enumerate(data):
	for element_index,element in enumerate(output_list):
		output_worksheet.write(list_index,element_index,element)
		
output_workbook.save(output_file)		
